#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;

int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* Count(void* args)
{
    pthread_detach(pthread_self());
    uint64_t i = (uint64_t)args;
    cout << "pthread: " << i << " thread create success!" << endl;

    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << "pthread: " << i << ", cnt: " << ++cnt << endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    for(uint64_t i = 0; i < 5; ++i)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,Count, (void*)i);
        usleep(1000);
    }
    
    while(true)
    {
        pthread_cond_signal(&cond);
        cout << "give a signal" << endl;
        sleep(1);
    }

    return 0;
}